function [ret] = set_motor_pwm(s, motor_a_dir, motor_a_pwm, motor_b_dir, motor_b_pwm)

serial_pause = 0.005;
byte_check = 9;

motor_a_pwm = round(motor_a_pwm);
motor_b_pwm = round(motor_b_pwm);

% Write to the serial port, only if it is open
done = 0;
while done == 0
    
    if strcmp(s.status, 'open')
    
        % Send the PWM command
        fprintf(s, ['pwm 1 ' num2str(motor_a_dir) ' ' num2str(motor_a_pwm) '\n']);
        pause(serial_pause);
                
        % Wait until the expected number of bytes is available                                  
        while s.BytesAvailable < byte_check
            pause(serial_pause);
        end   
        while s.BytesAvailable > 4            
            [~] = fscanf(s);            
            pause(serial_pause);
        end   
        
        if s.BytesAvailable ~= 4
            disp('PWM1 Yikes!');
        end
        
        % Send the PWM command
        fprintf(s, ['pwm 2 ' num2str(motor_b_dir) ' ' num2str(motor_b_pwm) '\n']);        
        pause(serial_pause);
        
        while s.BytesAvailable < byte_check
            pause(serial_pause);
        end           
        while s.BytesAvailable > 4            
            [~] = fscanf(s);
            pause(serial_pause);
        end   
        
        if s.BytesAvailable ~= 4
            disp('PWM2 Yikes!');
        end
        
        done = 1;
    else
        disp('Serial port not open');
        ret = nan;
    end
end
